/**
 * 公共组件js
 * 为每个组件页面提供公共js方法
 * @author 003598
 */

/**
 * 添加String的format方法 替换对应位置的字符串
 * 
 * @param array[] 数组
 */
String.prototype.format = function(array) {
	return this.replace(/\{(\d+)\}/g, function(s, i) {
		return array[i];
	});
};

/** 页面通用方法接口*/
var base ={
	/** form表单提交 */
	setAction : function(action, method, formId, target) {
		var actionHref = '';
		if(basePath==null||basePath==''){
			if(action.indexOf('/')==0){
				actionHref = action;
			}else{
				actionHref = "/" + action;
			}
		}else{
			actionHref = basePath + "/" +action;
		}
		method = method || "post";
		formId = "#" + (formId || "mainForm");
		target = target || "_self";
		$(formId).attr("action", actionHref).attr("method", method).attr("target",
				target).submit();
	},
	
	/** get提交*/
	getAction:function(action){
		if(basePath==null||basePath==''){
			if(action.indexOf('/')==0){
				window.location.href = action;
			}else{
				window.location.href = "/" + action;
			}
		}else{
			window.location.href= basePath + "/" +action;
		}
	},
	
	/** 验证input是否为空*/
	validInputNull: function($erroreDom){
		var flag = false;
		$('input:visible').each(function() {
			var _val = $(this).val();
			var _tip = $(this).attr('tip');
			if (!_val || '' === _val){
				$erroreDom.empty().append('<span>*</span><span>'+_tip+'</span>').show();
				flag = true;
				return false;
			}
		});
		return flag;
	},

	/** 验证input是否为空*/
	isInputNull: function(){
		var flag = false;
		$('input:visible').each(function() {
			var _val = $(this).val();
			var _tip = $(this).attr('tip');
			if (!_val || '' === _val){
				flag = true;
				
				Message.showMessage({
					text: _tip,
					textSize: 'left',
					titleSize: 'left'
				});
				
				return false;
			}
		});
		return flag;
	},
	
	/** 验证imei */
	isIMEI :function(s)
	{ 
		var etal = /^[0-9]{15}$/;
		if (!etal.test(s)){
			return false;  
		}
		
		var sum = 0; 
		var mul = 2; 
		var l = 14; 
		
		for (var i = 0; i < l; i++){  
			
			var digit = s.substring(l-i-1,l-i); 
			
			var tp = parseInt(digit,10)*mul; 
			
			if (tp == 10) {
				sum += (tp % 10) +1; 
			}
			else{
				sum += tp; 
			} 
			
			if (mul == 1){
				mul++; 
			} 
			else{
				mul--; 
			} 
		} 
		var chk = ((10 - (sum % 10)) % 10); 

		if (chk != parseInt(s.substring(14,15),10)){
			return false; 
		} 
		return true;
	},
	
	
	/** ajax公共方法 */
	AJAX_POST : function(action, data,type,callback,async) {
		var actionHref = '';
		if(basePath==null||basePath==''){
			if(action.indexOf('/')==0){
				actionHref = action;
			}else{
				actionHref = "/" + action;
			}
		}else{
			actionHref = basePath + "/" +action;
		}
		var dataObj = {};
		if (data){
			dataObj = data;
		}
		if(!type){
			type = 'json';
		}
		var as = async || true;
		if(async&&async=='false'){
			as = false;
		}else if(async&&async=='true'){
			as = true;
		}
		$.ajax({
			url : actionHref,
			data : dataObj,
			type : 'post',
			dataType : type,
			async : as,      // 取消异步请求
			beforeSend : function() {
				
			},
			success : function(o) {
				if (callback && $.isFunction(callback)) {
					callback(o);
				}
			},
			complete : function(XMLHttpRequest, textStatus) {
			},
			error : function() {
			}
			
		});
	},
	
	/** 验证手机号码*/
	vailNum:function(value){
		/** 手机号 */
		var strArray = [// 移动
		                '139', '138', '137', '136', '135', '134', '147', '150', '151', '152', '157', '158', '159', '182', '183', '184', '187', '188',
		                // 联通
		                '130', '131', '132', '155', '156', '185', '186', '145',
		                // 电信
		                '133', '153', '180', '181', '189', '177'];
		
		var mobile = new RegExp( '^(' + strArray.join('|') + ')\\d{8}$' ); 
		
		return mobile.test($.trim(value)); 
		
	},
	
};


var rdk = rdk || {};

rdk.comBtn = {
	init: function(){
		this.btn.init();
	},
	
	btn: {
		init:function(){
			this.helpBtn();
			this.close();
			this.repairInfoBtn();
		},
		
		helpBtn: function(){
			$('.help').click(function() {
				var tipHtml='<img src="'+basePath+'/resource/images/help.png" style="width: 100%;"/>';
				Message.showMessage({
					text: tipHtml,
					textSize: 'left',
					titleSize: 'left',
					title:""
				});
			});
		},
		repairInfoBtn: function(){
			$('.repairInfo').click(function() {
				var tipHtml='<p>1、30日之内激活的iPhone 才可以享受核桃提供的屏幕保修；</p>'+
				'<p>2、须在“桃核”微信公众账号将iPhone IMEI与“桃核”二维码ID完成绑定；</p>'+
				'<p>3、自完成绑定后1年内，iPhone因任何原因导致的屏幕碎裂，可免费更换一次屏幕；</p>'+
				'<p>4、屏幕维修服务，部分区域可免费上门维修，其他区域需寄送维修点（寄送维修1-2日完成，不含物流时间），具体维修服务以微信公众账号公布为准；</p>' +
				'<p>5、更换的屏幕为苹果原厂屏，内屏外屏全部更换；更换的屏幕终身质保（人为损坏除外）；</p>' +
				'<p>6、在绑定桃核之前屏幕已碎裂的手机，不再保修范围之内。</p>';
				
				Message.showMessage({
					title:"维保说明",
					text: tipHtml,
					textSize: 'left',
					titleSize: 'left'
				});
			});
		},
		close:function(){
			$('.close').click(function(){
				WeixinJSBridge.call('closeWindow');
			});
		}
		
	}
		
};



rdk.wechatJs = {
	wechatConfig: function(){
		
		var appId = $('#appid').val();
		var timestamp = $('#timestamp').val();
		var nonceStr = $('#nonceStr').val();
		var signature = $('#signature').val();
		wx.config({
		    debug: false,
		    appId: appId,
		    timestamp:timestamp,
		    nonceStr: nonceStr,
		    signature: signature,
		    jsApiList: [
		      'checkJsApi',
		      'onMenuShareTimeline',
		      'onMenuShareAppMessage',
		      'onMenuShareQQ',
		      'onMenuShareWeibo',
		      'onMenuShareQZone',
		      'hideMenuItems',
		      'showMenuItems',
		      'hideAllNonBaseMenuItem',
		      'showAllNonBaseMenuItem',
		      'translateVoice',
		      'startRecord',
		      'stopRecord',
		      'onRecordEnd',
		      'playVoice',
		      'pauseVoice',
		      'stopVoice',
		      'uploadVoice',
		      'downloadVoice',
		      'chooseImage',
		      'previewImage',
		      'uploadImage',
		      'downloadImage',
		      'getNetworkType',
		      'openLocation',
		      'getLocation',
		      'hideOptionMenu',
		      'showOptionMenu',
		      'closeWindow',
		      'scanQRCode',
		      'chooseWXPay',
		      'openProductSpecificView',
		      'addCard',
		      'chooseCard',
		      'openCard'
		    ]
		});
	},
};


$(function(){
	rdk.comBtn.init();
});




